<template>
  <div class="main">

    <!--搜索部分-->
    <div class="user-search-container">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
        </el-col>
        <el-col :span="1.5">
          <el-button type="primary" icon="el-icon-plus" size="mini" @click="showBlack = true">添加</el-button>
        </el-col>
      </el-row>

    </div>

    <!--列表部分-->
    <div class="user-list-container">

      <van-divider />

      <el-table v-loading="loading" :data="list">
        <el-table-column type="selection" width="50" align="center" />
        <el-table-column label="用户" align="center" prop="userId"/>
        <el-table-column label="黑名单" align="center" prop="blackId"/>
        <el-table-column label="创建时间" align="center" prop="createTime"/>


        <el-table-column label="操作" align="center" width="160" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="delete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total>0" :total="total" :page.sync="params.page" :limit.sync="params.limit" @pagination="onLoad" />
    </div>

    <el-dialog title="添加" :visible.sync="showBlack">
      <el-form >

        <el-form-item label="用户名" >
          <el-input v-model="blackUser" placeholder="请输入用户名"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showBlack = false">取 消</el-button>
        <el-button type="primary" @click="add(blackUser)">确 定</el-button>
      </div>
    </el-dialog>


  </div>
</template>
<script>
  import { blackPage} from '@/api/friend';
  import commentUtil from "@/utils/commentUtil";
  import {black} from "../../../api/friend";
  import {SUCCESS} from "../../../comm/constant";

  export default {


    data() {
      return {
        params:{
          username: null,
          limit: 10,
          page: 1,
        },
        dateRange: [],
        total:0,
        list: [],
        loading: false,
        isLoading: false,
        username: null,
        showBlack: false,
        blackUser: null,

      }
    },
    methods: {

      queryList(isSearch){//
        if(this.isLoading) return;
        this.isLoading = true;
        if(isSearch)this.params.page = 1;

        blackPage(this.params).then(res => {
          if(res.code!==SUCCESS){
            this.$notify({ type: 'danger', message: res.msg });
            return;
          }
          this.list = res.data.records;
          this.total = res.data.total;
          this.loading = false;
          this.isLoading = false;
        });
      },
      onClickLeft() {
        this.$router.go(-1);
      },
      onLoad() {
        console.info("加载数据中 第" + this.params.page + "页" )
        this.queryList(false)
      },
      delete(item) {
        this.black(item.userId,item.blackId,0)
      },
      add(blackUser){
        this.black(this.params.username,blackUser,1);
      },
      black(username, blackUser, isDelete) {
        let params = {};
        params.username = username;
        params.blackUser = blackUser;
        params.black = isDelete;
        black(params).then(res => {
          if(res.code!==SUCCESS){
            this.$notify({ type: 'danger', message: res.msg });
            return;
          }
          commentUtil.showAlertDialog(this,"操作成功!")
        });
      },
    },
    mounted() {
      this.params.username = this.$route.query.username;
      this.onLoad();
    },

  }
</script>
<style scoped>


  .user-search-container, .user-list-container{
    margin: 8px 17px;
  }
  .user-search-container .van-cell{
    margin: 8px 0px;
  }

  .user-search-container .van-dropdown-menu{
    height: 40px;
    margin: 8px 0px;
  }
  .user-search-container .van-button{
    height: 85px;
    width: 100px;
    background-color: #eeeeee;
    margin: 8px 0px;
  }
  .user-search-container .van-field, .user-search-container .van-cell{
    border: 1px solid #eeeeee;
  }
</style>
